<template>
	<view>
		<!--基础信息-->
		<view class="btName">基础信息</view>
		<view class="flex-white-plr26 ptb10 bdb_f5">
			<text class="mr26">
				<text>客户名称</text>
				<text class="redXingh">*</text>
			</text>
			<u-input v-model="name" placeholder="请输入客户名称" />
		</view>
		<view class="flex-white-plr26 ptb10 bdb_f5">
			<text class="mr26">
				<text>联系人</text>
				<text class="redXingh">*</text>
			</text>
			<u-input v-model="linkman" placeholder="请输入客户联系人" />
		</view>
		<view class="flex-white-plr26 ptb10 bdb_f5">
			<text class="mr26">手机号码
				<text class="redXingh">*</text>
			</text>
			<u-input v-model="mobile" placeholder="请输入联系方式" />
		</view>
		<view class="flex-white-plr26 ptb10 bdb_f5">
			<text class="mr26">QQ号码</text>
			<u-input v-model="qicq" placeholder="请输入QQ号码" />
		</view>
		<view class="flex-white-plr26 ptb10 bdb_f5">
			<text class="mr26">微信号码</text>
			<u-input v-model="weixin" placeholder="请输入微信号码" />
		</view>
		<view @click="pickerSelectFun('客户来源')" class="flex-white-plr26 ptb20 bdb_f5">
			<text class="mr26">线索来源</text>
			<view :class="source ? '' : 'cBlack'">
				{{source ? source : '请选择'}}
				<u-icon class="ml26" name="arrow-right" size="40" color="#888888"></u-icon>
			</view>
		</view>
<!-- 		<view @click="pickerSelectFun('线索状态')" class="flex-white-plr26 ptb20 bdb_f5">
			<text class="mr26">线索状态</text>
			<view :class="status ? '' : 'cBlack'">
				{{status ? status : '请选择'}}
				<u-icon class="ml26" name="arrow-right" size="40" color="#888888"></u-icon>
			</view>
		</view> -->

		<view class="flex-white-plr26 ptb20 bdb_f5" v-if="openstatus == 0">
			<text class="mr26">受理部门</text>
			<view class="ownerdept">
				<uni-data-picker :localdata="dept_list_items" popup-title="请选部门" @change="onchange" @nodeclick="onnodeclick">
				</uni-data-picker>
			</view>
		</view>

		<!-- 下拉选择 -->
		<u-select v-model="selectShow" :list="selectList" @confirm="selectConfirmFun"></u-select>

		<view class="flex-white-plr26 ptb10 bdb_f5">
			<text class="mr26">联系地址</text>
			<u-input v-model="address" placeholder="请输入联系地址" />
		</view>
		<view class="flex-white-plr26 ptb20 mt32">
			<u-input v-model="remark" placeholder="备注" maxlength="500" placeholderStyle="color: #303133;"
				type="textarea" />
		</view>
		<!--提交按钮-->
		<view class="submitView">
			<u-button type="primary" class="submitBtn" :ripple="true" ripple-bg-color="#909399" @click="submitBtn">
				{{pageType =='' ? '提交' : '保存'}}
			</u-button>
		</view>
	</view>
</template>

<script>
	let that = '';
	import {
		apiCstDict,
		apiClue
	} from '@/api/crm.js'
	import {
		apiSysDept
	} from '@/api/admin.js'
	export default {
		data() {
			return {
				cardIndex: '',
				xsName: '',
				name: '',
				linkman: '',
				qicq: '',
				weixin: '',
				mobile: '',
				address: '',
				remark: '',
				birthday: '',
				source: '',
				status_id: '1',
				status: '',
				openstatus:'1',
				selectShow: false,
				selectList: [], //下拉选择数据
				selectType: '',
				owner_dept_id: '',
				owner_user_id: uni.$userInfo.id, //归属员工id
				fuZeRen: uni.$userInfo.realname,
				xtRen: '',
				pageType: '', //当前页操作类型，add/update
				xgInfoObj: {},
				isAdd: 0,
				fkId: '',
				zhuanWeiClientId: '',
				cstDict: '', //所有字典
				dept_list_items: []
			}
		},
		onLoad(e) {
			that = this;
			that.cardIndex = e.index || '';
			if (e.type == 'update' || e.type == 'zhuanClient') {
				that.setDataFun();
				that.pageType = e.type;
				if (e.type == 'update') {
					uni.setNavigationBarTitle({
						title: '修改线索信息'
					})
				} else {
					uni.setNavigationBarTitle({
						title: '线索转为客户'
					})
				}
				console.log(that.pageType);

			}
			
			if (e.openstatus == '0') {
				that.openstatus=e.openstatus
			}

			
			that.getSysDept();//加载部门
			that.getCstDict();//加载字典数组

			if (e.isAdd) {
				that.isAdd = e.isAdd;
				if (uni.$addInfo) {
					that.linkman = uni.$addInfo.linkman;
					that.mobile = uni.$addInfo.mobile;
					that.name = uni.$addInfo.name;
					that.qicq = uni.$addInfo.qicq || '';
					that.address = uni.$addInfo.address || '';
					that.source = uni.$addInfo.source || '';
					that.weixin = uni.$addInfo.weixin || '';
					if (that.region) {
						let regionArr = (that.region).split(" ");
						that.province = regionArr[0];
						that.city = regionArr[1];
						that.area = regionArr[2];
					}
					if (uni.$addInfo.fkId) {
						that.fkId = uni.$addInfo.fkId;
					}
				}
			}
		},
		methods: {
			onchange(e) {
				const value = e.detail.value
			},
			onnodeclick(node) {
				console.log('这是点击')
				console.log(node)
				that.owner_dept_id=node.value
			},
			//下拉选择框架
			pickerSelectFun: function(str) {
				if (str == '线索状态') {
					that.selectList = [{
							value: 1,
							label: '初步意向'
						},
						{
							value: 2,
							label: '下次邀约'
						},
						{
							value: 3,
							abel: '转为客户'
						}
					];
				} else if (str == '客户阶段') {
					that.selectList = that.cstDict.salestage;
				} else if (str == '客户来源') {
					that.selectList = that.cstDict.source
				}
				that.selectShow = true;
				that.selectType = str;
			},
			selectConfirmFun: function(e) {
				let str = e[0].label;
				let val = e[0].value;
				if (that.selectType == '客户阶段') {
					that.salestage = str;
				} else if (that.selectType == '线索状态') {
					that.status = str;
					that.status_id = val;
				} else if (that.selectType == '客户来源') {
					that.source = str;
				}
			},
			// 提交按钮
			submitBtn: function() {
				if (that.name == '') {
					uni.showModal({
						title: '提示',
						content: '客户名称不能为空，请输入！',
						showCancel: false
					})
					return
				}
				if (that.linkman == '') {
					uni.showModal({
						title: '提示',
						content: '客户联系人不能为空，请输入！',
						showCancel: false
					})
					return
				}
				if (that.mobile == '') {
					uni.showModal({
						title: '提示',
						content: '客户联系方式不能为空，请输入！',
						showCancel: false
					})
					return
				}
				//请求参数
				let reqObj = {
					req: {
						name: that.name,
						linkman: that.linkman,
						mobile: that.mobile,
						qicq: that.qicq,
						weixin: that.weixin,
						source: that.source,
						status: that.status_id,
						address: that.address,
						remark: that.remark,
						owner_dept_id: that.owner_dept_id,
						owner_user_id: that.owner_user_id,
						create_user_id: that.owner_user_id
					}
				}

				let action = 'add';
				if (that.pageType == 'update') {
					action = 'update'
					reqObj.req.id = that.xgInfoObj.id;
					uni.showLoading({
						title: '修改中...'
					})

				} else if (that.pageType == 'zhuanClient') {
					
					action = 'goto_customer';
					
					let obj = reqObj.req;
					reqObj.req.create_user_id = uni.$userInfo.id;
					reqObj.req.owner_user_id = uni.$userInfo.id;
					reqObj.req.openstatus = 1; //默认为私有
					reqObj.req.clue_id = that.xgInfoObj.id; //线索编号
					reqObj.req.id = that.xgInfoObj.id; //线索编号

					reqObj.req = obj;

					uni.showLoading({
						title: '保存中...'
					})
				} else {
					reqObj.req.create_user_id = uni.$userInfo.id;
					reqObj.req.owner_user_id = uni.$userInfo.id;
					reqObj.req.openstatus = that.openstatus; //默认为私有
					uni.showLoading({
						title: '提交中...'
					})
				}
				let reqData = {
					action: action,
					params: reqObj
				}

				console.log(action);

				apiClue(reqData)
					.then(res => {
						if (action == 'update') {
							uni.showToast({
								title: '线索修改成功',
								icon: 'none',
								duration: 888,
								mask: true
							})
							//更新列表
							if (that.cardIndex != '') {
								uni.$emit('updateListByIndex', {
									index: that.cardIndex,
									obj: reqObj.req
								});
							}
							setTimeout(() => {
								uni.navigateBack()
							}, 888)

						} else if (action == 'goto_customer') {
							uni.showToast({
								title: '线索转为客户成功',
								icon: 'none',
								duration: 888,
								mask: true
							})
							//更新列表
							if (that.cardIndex != '') {
								uni.$emit('updateListByIndex', {
									index: that.cardIndex,
									obj: reqObj.req
								});
							}

							uni.$khInfo = reqObj.req;
							uni.redirectTo({
								url: '../cst_customer/detail?index=0'
							})


						} else {
							uni.showToast({
								title: '线索新增成功',
								icon: 'none',
								duration: 1000,
								mask: true
							})

							let addObj = reqObj.req;
							addObj.id = res.data; //增加返回id
							uni.$khInfo = addObj; //更新值

							if (that.isAdd) {
								uni.$emit('addItemInListFun', addObj)
							}
							setTimeout(() => {
								addObj.cjRenName = uni.$userInfo.nickname;
								addObj.fuZeRenName = uni.$userInfo.nickname;
								if(that.openstatus=='0'){
									uni.redirectTo({
										url: './show_public'
									})
								}else{
									uni.redirectTo({
										url: './show'
									})
								}
								
							}, 188)
						}
					})
			},
			// 初始化修改数据
			setDataFun: function() {
				let obj = uni.$khInfo;
				that.xgInfoObj = uni.$khInfo;
				that.name = obj.name;
				that.linkman = obj.linkman;
				that.mobile = obj.mobile;
				that.weixin = obj.weixin;
				that.qicq = obj.qicq;
				that.address = obj.address;
				that.remark = obj.remark;
			},
			// 获得部门数据
			getSysDept: function() {
				let that = this;
				let reqData = {
					action: 'get_list_tree',
					params: {
			
					}
				}
				apiSysDept(reqData)
					.then(res => {
						console.log(res.data);
						let dataArr = res.data;
						let rowData = [];
						for (var key in dataArr) {
							rowData.push({
								name: dataArr[key].name,
								id: dataArr[key].id
							})
						}
						that.dept_list_items = res.data;
					});
			},
			//载入字典
			getCstDict:function(){
				let that = this;
				that.cstDict = uni.getStorageSync('cstDict') || '';
				console.log('载入字典');
				console.log(that.cstDict);
				
				if (that.cstDict) {
					let reqData = {
						action: 'get_list',
						params: {
				
						}
					}
					apiCstDict(reqData)
						.then(res => {
							let dictArr = res.data;
							let newArr = [];
							for (var key in dictArr) {
								let rowData = [];
								for (var i = 0; i < dictArr[key].length; i++) {
									rowData.push({
										label: dictArr[key][i].name,
										value: dictArr[key][i].id
									})
								}
								newArr[key] = rowData;
							}
							that.cstDict = newArr;
						});
				}
			}
		}
	}
</script>

<style>
	page {
		background-color: #F8F8F8;
		padding-bottom: 260rpx;
	}

	.u-radio {
		width: 200rpx !important;
	}

	.btName {
		margin-bottom: 8rpx;
	}
	.ownerdept{border: solid 1px #ffffff !important;}
	/deep/ .input-value-border{border:solid 1px #ffffff !important;}
</style>
